﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.SqlClient;
using System.Linq;
using System.Text;

namespace EttainGroup.Filters
{
    public class DatabaseFilterRepository : IFilterRepository
    {
        private string ConnectionString;
        private FilterDBDataContext ctx;
        public DatabaseFilterRepository(string connectionString)
        {
            ConnectionString = connectionString;
            ctx = new FilterDBDataContext(connectionString);
        }
        public List<FilterDefinition> FiltersForExpression(string typeName, string componentName, string expressionId)
        {
            var filters = (from f in ctx.Filters
                          where 
                          //f.filterComponentName.Equals(componentName)
                          //&& f.entity.Equals(typeName)
                          f.filterGroupId == Convert.ToInt32(expressionId)
                          select new Filters.FilterDefinition()
                          {
                              ComparisonType = (FilterComparisonType)f.comparisonType,
                              EntityName = f.entity,
                              Group = f.filterGroupId.ToString(),
                              PropertyName = f.property,
                              Values = f.FilterValues.Select(fv => new FilterDefinitionValue() { Value = fv.primaryValue, Secondary = fv.secondaryValue }).ToList(),
                              ValueType=(FilterValueType)f.propertytype
                          }).ToList();
            return filters;
        }

        private List<string> GetExpressionGroupIds(string componentName, string externalEntityId) {
            var groupIds = (from fg in ctx.FilterGroups
                           where fg.componentName.Equals(componentName)
                           && fg.entityId == Convert.ToInt32(externalEntityId)
                           select fg.id.ToString()).ToList();

            return groupIds;
        }

        public System.Linq.Expressions.Expression<Func<T, bool>> BuildPredicate<T>(System.Linq.Expressions.Expression<Func<T, bool>> additionalFilter, string componentName, string groupId)
        {
            return Utility.BuildPredicate<T>(this, additionalFilter, componentName, groupId);
        }

        public List<System.Linq.Expressions.Expression<Func<T, bool>>> GetExpressionListForGroup<T>(System.Linq.Expressions.Expression<Func<T, bool>> additionalFilter, string componentName, string externalEntityId) {
            List<System.Linq.Expressions.Expression<Func<T, bool>>> expressions = new List<System.Linq.Expressions.Expression<Func<T,bool>>>();
            foreach (string item in GetExpressionGroupIds(componentName, externalEntityId))
            {
                var filter = BuildPredicate<T>(additionalFilter, componentName, item);
                expressions.Add(filter);
            }
            return expressions;
        }
    }
}
